package com.aspose.cells.b.a.d;

import java.io.DataInput;
import java.io.DataInputStream;
import java.io.DataOutput;
import java.io.EOFException;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.io.UTFDataFormatException;

public class zl implements DataInput, DataOutput {
   protected RandomAccessFile a;
   protected long b;
   protected byte[] c;
   protected long d;
   protected long e;
   protected int f;
   protected boolean g;
   protected int h;
   boolean i;
   private String j;

   public zl(String var1, String var2) throws IOException {
      this(var1, var2, 4096);
   }

   public zl(String var1, String var2, int var3) throws IOException {
      this(var1, "r".equals(var2) ? 1 : ("rw".equals(var2) ? 3 : 0), var3);
   }

   public zl(File var1, String var2) throws IOException {
      this(var1.getPath(), var2);
   }

   public zl(String var1, int var2, int var3) throws FileNotFoundException, IOException {
      this.i = false;
      this.h = var2;
      var2 |= 1;
      if ((this.h & 4) > 0) {
         this.h |= 2;
      }

      File var4 = new File(var1);
      if ((this.h & 2) > 0 && !var4.exists()) {
         var2 |= 4;
      }

      if ((this.h & 4) > 0 && var4.exists() && !var4.delete()) {
         throw new IOException("Failed to delete " + var1);
      } else if (this.h == 1 && !(new File(var1)).exists()) {
         throw new FileNotFoundException(var1);
      } else {
         String var5 = (this.h & 2) > 0 ? "rw" : "r";
         this.a = new RandomAccessFile(var1, var5);
         this.d = 0L;
         this.e = 0L;
         this.f = 0;
         this.b = 0L;
         this.c = new byte[var3];
         this.g = false;
         this.j = var1;
      }
   }

   public String a() {
      return this.j;
   }

   public void b() throws IOException {
      if (this.a != null) {
         if ((this.h | 2) > 0 && this.i) {
            this.a.seek(this.d);
            this.a.write(this.c, 0, this.f);
         }

         this.a.close();
         this.a = null;
         this.c = null;
      }
   }

   public void a(long var1) throws IOException {
      if (var1 >= this.d && var1 < this.e) {
         this.b = var1;
      } else {
         if (this.i) {
            this.e();
         }

         this.d = var1;
         this.b = var1;
         this.f = this.a(var1, this.c, 0, this.c.length);
         if (this.f < 0) {
            this.f = 0;
            this.g = true;
         } else {
            this.g = false;
         }

         this.e = this.d + (long)this.f;
      }
   }

   public long c() throws IOException {
      return this.b;
   }

   public long d() throws IOException {
      long var1 = this.a.length();
      return var1 < this.e ? this.e : var1;
   }

   public void b(long var1) throws IOException {
      if (this.i) {
         this.e();
      }

      this.a.setLength(var1);
      if (this.b > var1) {
         this.a(var1);
      }

   }

   public void e() throws IOException {
      if (this.i) {
         this.a.seek(this.d);
         this.a.write(this.c, 0, this.f);
         this.i = false;
      }

   }

   public final int f() throws IOException {
      if (this.b < this.e) {
         return this.c[(int)(this.b++ - this.d)] & 255;
      } else if (this.g) {
         return -1;
      } else {
         this.a(this.b);
         return this.f();
      }
   }

   private int c(byte[] var1, int var2, int var3) throws IOException {
      if (this.g) {
         return -1;
      } else {
         int var4 = (int)(this.e - this.b);
         if (var4 < 1) {
            this.a(this.b);
            return this.c(var1, var2, var3);
         } else {
            int var5 = var4 >= var3 ? var3 : var4;
            System.arraycopy(this.c, (int)(this.b - this.d), var1, var2, var5);
            this.b += (long)var5;
            if (var5 < var3) {
               int var6 = var3 - var5;
               if (var6 > this.c.length) {
                  var6 = this.a(this.b, var1, var2 + var5, var3 - var5);
               } else {
                  this.a(this.b);
                  if (!this.g) {
                     var6 = var6 > this.f ? this.f : var6;
                     System.arraycopy(this.c, 0, var1, var2 + var5, var6);
                  } else {
                     var6 = -1;
                  }
               }

               if (var6 > 0) {
                  this.b += (long)var6;
                  return var5 + var6;
               }
            }

            return var5;
         }
      }
   }

   protected int a(long var1, byte[] var3, int var4, int var5) throws IOException {
      this.a.seek(var1);
      int var6 = this.a.read(var3, var4, var5);
      return var6;
   }

   public int a(byte[] var1, int var2, int var3) throws IOException {
      return this.c(var1, var2, var3);
   }

   public final void readFully(byte[] b) throws IOException {
      this.readFully(b, 0, b.length);
   }

   public final void readFully(byte[] b, int off, int len) throws IOException {
      int var5;
      for(int var4 = 0; var4 < len; var4 += var5) {
         var5 = this.a(b, off + var4, len - var4);
         if (var5 < 0) {
            throw new EOFException();
         }
      }

   }

   public int skipBytes(int n) throws IOException {
      this.a(this.c() + (long)n);
      return n;
   }

   public final void write(int b) throws IOException {
      if (this.b >= this.d && this.b < this.d + (long)this.c.length) {
         this.c[(int)(this.b - this.d)] = (byte)b;
         this.i = true;
         if (this.b >= this.e) {
            ++this.f;
            ++this.e;
         }

         ++this.b;
      } else {
         this.a(this.b);
         this.write(b);
      }

   }

   public final void b(byte[] var1, int var2, int var3) throws IOException {
      if (var3 < this.c.length) {
         int var4 = 0;
         int var5 = 0;
         if (this.b >= this.d) {
            var4 = (int)(this.d + (long)this.c.length - this.b);
         }

         long var6;
         if (var4 > 0) {
            var5 = var4 > var3 ? var3 : var4;
            System.arraycopy(var1, var2, this.c, (int)(this.b - this.d), var5);
            this.i = true;
            var6 = this.b + (long)var5;
            this.e = var6 > this.e ? var6 : this.e;
            this.f = (int)(this.e - this.d);
            this.b += (long)var5;
         }

         if (var5 < var3) {
            this.a(this.b);
            System.arraycopy(var1, var2 + var5, this.c, (int)(this.b - this.d), var3 - var5);
            this.i = true;
            var6 = this.b + (long)(var3 - var5);
            this.e = var6 > this.e ? var6 : this.e;
            this.f = (int)(this.e - this.d);
            this.b += (long)(var3 - var5);
         }
      } else {
         if (this.i) {
            this.e();
            this.d = this.e = (long)(this.f = 0);
            this.a.seek(this.b);
         }

         this.a.write(var1, var2, var3);
         this.b += (long)var3;
      }

   }

   public void write(byte[] b) throws IOException {
      this.b(b, 0, b.length);
   }

   public void write(byte[] b, int off, int len) throws IOException {
      this.b(b, off, len);
   }

   public final boolean readBoolean() throws IOException {
      int var1 = this.f();
      if (var1 < 0) {
         throw new EOFException();
      } else {
         return var1 != 0;
      }
   }

   public final byte readByte() throws IOException {
      int var1 = this.f();
      if (var1 < 0) {
         throw new EOFException();
      } else {
         return (byte)var1;
      }
   }

   public final int readUnsignedByte() throws IOException {
      int var1 = this.f();
      if (var1 < 0) {
         throw new EOFException();
      } else {
         return var1;
      }
   }

   public final short readShort() throws IOException {
      int var1 = this.f();
      int var2 = this.f();
      if ((var1 | var2) < 0) {
         throw new EOFException();
      } else {
         return (short)((var1 << 8) + (var2 << 0));
      }
   }

   public final int readUnsignedShort() throws IOException {
      int var1 = this.f();
      int var2 = this.f();
      if ((var1 | var2) < 0) {
         throw new EOFException();
      } else {
         return (var1 << 8) + (var2 << 0);
      }
   }

   public final char readChar() throws IOException {
      int var1 = this.f();
      int var2 = this.f();
      if ((var1 | var2) < 0) {
         throw new EOFException();
      } else {
         return (char)((var1 << 8) + (var2 << 0));
      }
   }

   public final int readInt() throws IOException {
      int var1 = this.f();
      int var2 = this.f();
      int var3 = this.f();
      int var4 = this.f();
      if ((var1 | var2 | var3 | var4) < 0) {
         throw new EOFException();
      } else {
         return (var1 << 24) + (var2 << 16) + (var3 << 8) + (var4 << 0);
      }
   }

   public final long readLong() throws IOException {
      return ((long)this.readInt() << 32) + ((long)this.readInt() & 4294967295L);
   }

   public final float readFloat() throws IOException {
      return Float.intBitsToFloat(this.readInt());
   }

   public final double readDouble() throws IOException {
      return Double.longBitsToDouble(this.readLong());
   }

   public final String readLine() throws IOException {
      StringBuffer var1 = new StringBuffer();

      int var2;
      while((var2 = this.f()) != -1 && var2 != 10) {
         var1.append((char)var2);
      }

      return var2 == -1 && var1.length() == 0 ? null : var1.toString();
   }

   public final String readUTF() throws IOException {
      return DataInputStream.readUTF(this);
   }

   public final void writeBoolean(boolean v) throws IOException {
      this.write(v ? 1 : 0);
   }

   public final void writeByte(int v) throws IOException {
      this.write(v);
   }

   public final void writeShort(int v) throws IOException {
      this.write(v >>> 8 & 255);
      this.write(v >>> 0 & 255);
   }

   public final void writeChar(int v) throws IOException {
      this.write(v >>> 8 & 255);
      this.write(v >>> 0 & 255);
   }

   public final void writeInt(int v) throws IOException {
      this.write(v >>> 24 & 255);
      this.write(v >>> 16 & 255);
      this.write(v >>> 8 & 255);
      this.write(v >>> 0 & 255);
   }

   public final void writeLong(long v) throws IOException {
      this.write((int)(v >>> 56) & 255);
      this.write((int)(v >>> 48) & 255);
      this.write((int)(v >>> 40) & 255);
      this.write((int)(v >>> 32) & 255);
      this.write((int)(v >>> 24) & 255);
      this.write((int)(v >>> 16) & 255);
      this.write((int)(v >>> 8) & 255);
      this.write((int)(v >>> 0) & 255);
   }

   public final void writeFloat(float v) throws IOException {
      this.writeInt(Float.floatToIntBits(v));
   }

   public final void writeDouble(double v) throws IOException {
      this.writeLong(Double.doubleToLongBits(v));
   }

   public final void writeBytes(String s) throws IOException {
      int var2 = s.length();

      for(int var3 = 0; var3 < var2; ++var3) {
         this.write((byte)s.charAt(var3));
      }

   }

   public final void writeChars(String s) throws IOException {
      int var2 = s.length();

      for(int var3 = 0; var3 < var2; ++var3) {
         char var4 = s.charAt(var3);
         this.write(var4 >>> 8 & 255);
         this.write(var4 >>> 0 & 255);
      }

   }

   public final void writeUTF(String str) throws IOException {
      int var2 = str.length();
      int var3 = 0;

      int var4;
      char var5;
      for(var4 = 0; var4 < var2; ++var4) {
         var5 = str.charAt(var4);
         if (var5 >= 1 && var5 <= 127) {
            ++var3;
         } else if (var5 > 2047) {
            var3 += 3;
         } else {
            var3 += 2;
         }
      }

      if (var3 > 65535) {
         throw new UTFDataFormatException();
      } else {
         this.write(var3 >>> 8 & 255);
         this.write(var3 >>> 0 & 255);

         for(var4 = 0; var4 < var2; ++var4) {
            var5 = str.charAt(var4);
            if (var5 >= 1 && var5 <= 127) {
               this.write(var5);
            } else if (var5 > 2047) {
               this.write(224 | var5 >> 12 & 15);
               this.write(128 | var5 >> 6 & 63);
               this.write(128 | var5 >> 0 & 63);
            } else {
               this.write(192 | var5 >> 6 & 31);
               this.write(128 | var5 >> 0 & 63);
            }
         }

      }
   }

   public String toString() {
      return "fp=" + this.b + ", bs=" + this.d + ", de=" + this.e + ", ds=" + this.f + ", bl=" + this.c.length + ", m=" + this.h + ", bm=" + this.i;
   }
}
